package com.itqf.factory;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.FactoryBean;

import java.io.InputStream;

/**
 * 姓名：刘珈麟
 * 项目名：spring-01
 * 日期：2021/6/27
 * 描述：工厂bean
 *      父类：FactoryBean<要生产的类型>
 *              生产另一个对象。
 */

public class MySqlSessionFactoryBean implements FactoryBean<SqlSessionFactory> {
    @Override
    public SqlSessionFactory getObject() throws Exception {
        System.out.println("生产SqlSessionnFactory");
        InputStream resource = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resource);
        return build;
    }

    @Override
    public Class<?> getObjectType() {
        //返回生产那个对象的类型
        return SqlSessionFactory.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
